package com.luke.star.persist.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import com.luke.star.bean.StTaskVo;
import com.luke.star.persist.po.StTask;
import com.luke.star.persist.po.StTaskExample;

public interface StTaskMapper {
    @SelectProvider(type=StTaskSqlProvider.class, method="countByExample")
    long countByExample(StTaskExample example);

    @DeleteProvider(type=StTaskSqlProvider.class, method="deleteByExample")
    int deleteByExample(StTaskExample example);

    @Delete({
        "delete from st_task",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @Insert({
        "insert into st_task (id, task_name, ",
        "state, expect_num, star_num, ",
        "task_desc, star_type, ",
        "create_time, update_time, ",
        "userid, weight, ",
        "task_type)",
        "values (#{id,jdbcType=VARCHAR}, #{taskName,jdbcType=VARCHAR}, ",
        "#{state,jdbcType=CHAR}, #{expectNum,jdbcType=INTEGER}, #{starNum,jdbcType=INTEGER}, ",
        "#{taskDesc,jdbcType=VARCHAR}, #{starType,jdbcType=CHAR}, ",
        "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, ",
        "#{userid,jdbcType=VARCHAR}, #{weight,jdbcType=INTEGER}, ",
        "#{taskType,jdbcType=CHAR})"
    })
    int insert(StTask record);

    @InsertProvider(type=StTaskSqlProvider.class, method="insertSelective")
    int insertSelective(StTask record);

    @SelectProvider(type=StTaskSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
        @Result(column="state", property="state", jdbcType=JdbcType.CHAR),
        @Result(column="expect_num", property="expectNum", jdbcType=JdbcType.INTEGER),
        @Result(column="star_num", property="starNum", jdbcType=JdbcType.INTEGER),
        @Result(column="task_desc", property="taskDesc", jdbcType=JdbcType.VARCHAR),
        @Result(column="star_type", property="starType", jdbcType=JdbcType.CHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="userid", property="userid", jdbcType=JdbcType.VARCHAR),
        @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
        @Result(column="task_type", property="taskType", jdbcType=JdbcType.CHAR)
    })
    List<StTask> selectByExample(StTaskExample example);

    @Select({
        "select",
        "id, task_name, state, expect_num, star_num, task_desc, star_type, create_time, ",
        "update_time, userid, weight, task_type",
        "from st_task",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
        @Result(column="state", property="state", jdbcType=JdbcType.CHAR),
        @Result(column="expect_num", property="expectNum", jdbcType=JdbcType.INTEGER),
        @Result(column="star_num", property="starNum", jdbcType=JdbcType.INTEGER),
        @Result(column="task_desc", property="taskDesc", jdbcType=JdbcType.VARCHAR),
        @Result(column="star_type", property="starType", jdbcType=JdbcType.CHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="userid", property="userid", jdbcType=JdbcType.VARCHAR),
        @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
        @Result(column="task_type", property="taskType", jdbcType=JdbcType.CHAR)
    })
    StTask selectByPrimaryKey(String id);

    @UpdateProvider(type=StTaskSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") StTask record, @Param("example") StTaskExample example);

    @UpdateProvider(type=StTaskSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") StTask record, @Param("example") StTaskExample example);

    @UpdateProvider(type=StTaskSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(StTask record);

    @Update({
        "update st_task",
        "set task_name = #{taskName,jdbcType=VARCHAR},",
          "state = #{state,jdbcType=CHAR},",
          "expect_num = #{expectNum,jdbcType=INTEGER},",
          "star_num = #{starNum,jdbcType=INTEGER},",
          "task_desc = #{taskDesc,jdbcType=VARCHAR},",
          "star_type = #{starType,jdbcType=CHAR},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "userid = #{userid,jdbcType=VARCHAR},",
          "weight = #{weight,jdbcType=INTEGER},",
          "task_type = #{taskType,jdbcType=CHAR}",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(StTask record);
    
    //-----------------------------------------by human
    
    @Select({
    	"select a.*,m.* ",
    	" from St_Task a left join st_assignment m on a.id=m.assi_task_id",
    	"where",
    	" ('${taskName}'='' OR a.task_name like '%${taskName}%')",
    	" order by create_time desc ",
    	" limit ${begin},${pageSize}",
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR),
        @Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
        @Result(column="state", property="state", jdbcType=JdbcType.CHAR),
        @Result(column="expect_num", property="expectNum", jdbcType=JdbcType.INTEGER),
        @Result(column="star_num", property="starNum", jdbcType=JdbcType.INTEGER),
        @Result(column="task_desc", property="taskDesc", jdbcType=JdbcType.VARCHAR),
        @Result(column="star_type", property="starType", jdbcType=JdbcType.CHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="userid", property="userid", jdbcType=JdbcType.VARCHAR),
        @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
        @Result(column="task_type", property="taskType", jdbcType=JdbcType.CHAR),
        @Result(column="assi_id", property="assiId", jdbcType=JdbcType.VARCHAR),
        @Result(column="assi_task_id", property="assiTaskId", jdbcType=JdbcType.VARCHAR),
        @Result(column="assi_create_time", property="assiCreateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="assi_update_time", property="assiUpdateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="assi_desc", property="assiDesc", jdbcType=JdbcType.VARCHAR),
        @Result(column="assi_state", property="assiState", jdbcType=JdbcType.CHAR),
        @Result(column="assi_expect_num", property="assiExpectNum", jdbcType=JdbcType.INTEGER),
        @Result(column="assi_task_model", property="assiTaskModel", jdbcType=JdbcType.CHAR),
        @Result(column="assi_circle_model", property="assiCircleModel", jdbcType=JdbcType.CHAR),
        @Result(column="assi_run_time", property="assiRunTime", jdbcType=JdbcType.INTEGER)
    })
    List<StTaskVo> listStTaskByPage(Map<String,Object> map);
    
	@Select({
    	"select count(1) from St_Task a ",
    	"where",
    	" ('${taskName}'='' OR a.task_name='${taskName}')",
	})
	int countStTaskByPage(Map<String,Object> map);    
	
	
	/**
	 * 模板消息查询
	 */
	@Select({
		"select note.add_star_num,t.* from st_task t",
		"join ",
		"(",
		"	select n.task_id,sum(n.add_star_num) add_star_num from st_task_note n",
		"	where n.create_time between  date_add(CURDATE(), interval -1 day) and CURDATE() ",
		"	group by n.task_id",
		") note",
		" on t.id=note.task_id",
		"where ",
		"t.userid is not null ",
		"and t.state='00'",
		"order by userid,task_name",
	})
	@Results({
		@Result(column="add_star_num", property="addStarNum", jdbcType=JdbcType.INTEGER),
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR),
        @Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
        @Result(column="state", property="state", jdbcType=JdbcType.CHAR),
        @Result(column="expect_num", property="expectNum", jdbcType=JdbcType.INTEGER),
        @Result(column="star_num", property="starNum", jdbcType=JdbcType.INTEGER),
        @Result(column="task_desc", property="taskDesc", jdbcType=JdbcType.VARCHAR),
        @Result(column="star_type", property="starType", jdbcType=JdbcType.CHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="userid", property="userid", jdbcType=JdbcType.VARCHAR),
        @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
        @Result(column="task_type", property="taskType", jdbcType=JdbcType.CHAR),
	})
	List<StTaskVo> queryYesterdayNote();
    
}